package com.xjh.controller;

import com.xjh.bo.StudentBO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/thymeleaf")
public class TestThymeleafController {
    // 前端访问这个接口，会返回给前端index.html模板，并且通过model将数据带给模板
    @GetMapping("/index")
    public String getindex(Model model) {



        // 创建一个对象
        StudentBO studentBO = new StudentBO();
        studentBO.setId(1L);
        studentBO.setStuName("张三");
        studentBO.setStuSex("男");

        StudentBO studentBO1 = new StudentBO();
        studentBO1.setId(2L);
        studentBO1.setStuName("李四");
        studentBO1.setStuSex("女");
        List<StudentBO> studentList = new ArrayList<>();
        studentList.add(studentBO1);
        studentList.add(studentBO);
        Map<String, String> map = new HashMap<>();
        map.put("place","博学谷");
        map.put("feeling","very well");

        /**数据添加到model中(attribute是key/value 形式，前端可根据key取出value)*/
        //普通字符串
        model.addAttribute("name", "建华");
        //储存javabean
        model.addAttribute("student",studentBO);
        //储存List
        model.addAttribute("studentList",studentList);
        //储存Map
        model.addAttribute("map",map);

        return "index";
    }

}
